package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.KillConstants;
import cn.itsource.ymcc.constant.OrderConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "KillConsumer",//消费者组名
        topic = KillConstants.TOPIC_KILL_COURSE_DELAY_MESSAGE,
        selectorExpression = KillConstants.TAGS_KILL_COURSE_DELAY_MESSAGE
)
public class KillConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderNo = new String(body, Constants.UTF8);
            //不管怎么样 都要删除伪订单   不能一直占用redis的空间
            redisTemplate.delete(KillConstants.KEY_PREFIX_PRE_ORDER+orderNo);
            //思考  用户的购买记录是否需要删除

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
